import * as globalTypes from '../../../../mutation-types'
import operationPBX from '../../../../api/config/callCenter/operationPBX'
export const checkPbxLoadInfo = ({commit, state, rootState, dispatch}, data) => {
  return operationPBX.checkPbxLoadInfo(data)
  .then((response) => {
    if (response.success) {
      let stateObj = {}
      if (response.loadCount < 200) {
        stateObj.title = '空闲'
        stateObj.type = 'success'
      } else if (response.loadCount < 350) {
        stateObj.title = '繁忙'
        stateObj.type = 'warning'
      } else if (response.loadCount <= 500) {
        stateObj.title = '拥挤'
        stateObj.type = 'important'
      } else if (response.loadCount > 500) {
        stateObj.title = '饱和'
        stateObj.type = 'inverse'
      } else {
        stateObj.title = '未知'
        stateObj.type = ''
      }
      return stateObj
    }
  })
  .catch(() => {
    commit(globalTypes.SET_ERROR, 'message.default_tips')
  })
}

export const checkPbxMachineLoadInfo = ({commit, state, rootState, dispatch}, data) => {
  return operationPBX.checkPbxLoadInfo(data)
  .then((response) => {
    if (response.success) {
      let stateObj = {}
      if (response.loadCount < 2000) {
        stateObj.title = '空闲'
        stateObj.type = 'success'
      } else if (response.loadCount < 3500) {
        stateObj.title = '繁忙'
        stateObj.type = 'warning'
      } else if (response.loadCount <= 5000) {
        stateObj.title = '拥挤'
        stateObj.type = 'important'
      } else if (response.loadCount > 5000) {
        stateObj.title = '饱和'
        stateObj.type = 'inverse'
      } else {
        stateObj.title = '未知'
        stateObj.type = ''
      }
      return stateObj
    }
  })
  .catch(() => {
    commit(globalTypes.SET_ERROR, 'message.default_tips')
  })
}

export const queryAccountPbxBoard = ({commit, state, rootState, dispatch}, data) => {
  return operationPBX.queryAccountPbxBoard(data)
  .then((response) => {
    if (response.success) {
      return response
    }
  })
  .catch(() => {
    commit(globalTypes.SET_ERROR, 'message.default_tips')
  })
}

export const removeAccountPbx = ({commit, state, rootState, dispatch}, data) => {
  return operationPBX.removeAccountPbx(data)
  .then((response) => {
    if (response.success) {
      return response
    }
  })
  .catch(() => {
    commit(globalTypes.SET_ERROR, 'message.default_tips')
  })
}

export const saveAccountPbx = ({commit, state, rootState, dispatch}, data) => {
  return operationPBX.saveAccountPbx(data)
  .then((response) => {
    if (response.success) {
      return response
    }
  })
  .catch(() => {
    commit(globalTypes.SET_ERROR, 'message.default_tips')
  })
}

export const queryPhoneBarConfig = ({commit, state, rootState, dispatch}, data) => {
  return operationPBX.queryPhoneBarConfig(data)
  .then((response) => {
    if (response.success) {
      return response
    }
  })
  .catch(() => {
    commit(globalTypes.SET_ERROR, 'message.default_tips')
  })
}

export const savePhoneBarConfig = ({commit, state, rootState, dispatch}, data) => {
  return operationPBX.savePhoneBarConfig(data)
  .then((response) => {
    if (response.success) {
      return response
    }
  })
  .catch(() => {
    commit(globalTypes.SET_ERROR, 'message.default_tips')
  })
}

export const moveAccountPbx = ({commit, state, rootState, dispatch}, data) => {
  return operationPBX.moveAccountPbx(data)
  .then((response) => {
    if (response.success) {
      return response
    } else {
      if (response.Reason === '501') {
        commit(globalTypes.SET_ERROR, 'message.move_account_pbx_error501')
      } else if (response.Reason === '502') {
        commit(globalTypes.SET_ERROR, 'message.move_account_pbx_error502')
      }
    }
  })
  .catch(() => {
    commit(globalTypes.SET_ERROR, 'message.default_tips')
  })
}
